package v1

import (
	"net/http"
	"hytx_media/pkg/app"

	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
	"hytx_media/pkg/util"
)

// 生成验证码
func GenerateCaptchaHandler(c *gin.Context) {
	appG := app.Gin{c}
	// get session
	//session := sessions.Default(c)
	captchaConfig := util.GetCaptchaConfig()
	//创建base64图像验证码
	config := captchaConfig.ConfigCharacter
	//GenerateCaptcha 第一个参数为空字符串,包会自动在服务器一个随机种子给你产生随机uiid.
	captchaId, digitCap := base64Captcha.GenerateCaptcha(captchaConfig.Id, config)
	base64Png := base64Captcha.CaptchaWriteToBase64Encoding(digitCap)
	//session.Set("captchaId", captchaId)

	//c.String(http.StatusOK, base64Png)
	appG.ResponseMsg(http.StatusOK, "true", map[string]string{"verify_id":captchaId, "verify_value":base64Png})
}

//  验证 验证码是否正确
func VerfiyCaptcha(c *gin.Context){
	appG := app.Gin{c}

	captchaId := c.PostForm("verify_id")
	verifyValue := c.PostForm("verify_value")

	verifyResult := base64Captcha.VerifyCaptcha(captchaId, verifyValue)
	if verifyResult {
		appG.ResponseMsg(http.StatusOK, "true", "验证码正确")
	} else {
		appG.ResponseMsg(http.StatusOK, "false", "验证码错误")
	}
}